//package kt.flows
//
//import kotlinx.coroutines.*
//
///**
// *
// * 4
// * CoroutineContext 代表了上下文，也可以说说我调度器
// * CoroutineDispatcher 线程调度器，他确定了协程在那个线程or在那些上执行
// *  调度器可以让协程在一个特定的线程执行，也可将他分派到一个线程池
// *
// * 不同线程之间的跳转
// * @see switchThreadContext
// *
// *  对coroutines重命名
// *  @sample nameCoroutineAsync
// *
// *  cancel
// *  [subLaunchJob]
// *
// *  join
// *  [parentsCoroutinesTask]
// *
// */
//fun main() {
////    dispatchLaunchSample()
////    launchDelayDemo()
////    multiAsync()
////    switchThreadContext()
////    threadJob()
////    subLaunchJob()
////    nameCoroutineAsync()
////    parentsCoroutinesTask()
////    lifeCoroutinesTest()
//    threadLocalCoroutinesTest()
//}
//
//
//val threadLocal = ThreadLocal<String?>() // 声明线程局部变量
//
//fun threadLocalCoroutinesTest(){
//    runBlocking {
//        threadLocal.set("main")
//
//        println("Pre-main, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
//
//        val job = launch(Dispatchers.Default + threadLocal.asContextElement(value = "launch")) {
//            println("Launch start, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
//
//            yield()
//
//            println("After yield, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
//        }
//
//        job.join()
//        println("Post-main, current thread: ${Thread.currentThread()}, thread local value: '${threadLocal.get()}'")
//
//    }
//
//}
//
//
//fun lifeCoroutinesTest(){
//    runBlocking {
//        //sampleStart
//        val activity = Activity()
//        activity.doSomething() // 运行测试函数
//
//        println("Launched coroutines")
//        delay(500L) // 延迟半秒钟
//
//        println("Destroying activity!")
//        activity.destroy() // 取消所有的协程
//
//        delay(1000) // 为了在视觉上确认它们没有工作
//    }
//
//}
//
//class Activity {
//    private val mainScope = CoroutineScope(Dispatchers.Default) // use Default for test purposes
//
//    fun destroy() {
//        mainScope.cancel()
//    }
//
//    fun doSomething() {
//        // 在示例中启动了 10 个协程，且每个都工作了不同的时长
//        repeat(10) { i ->
//            mainScope.launch {
//                delay((i + 1) * 200L) // 延迟 200 毫秒、400 毫秒、600 毫秒等等不同的时间
//                println("Coroutine $i is done")
//            }
//        }
//    }
//} // Activity 类结束
//
//
//
//fun parentsCoroutinesTask(){
//    runBlocking {
//        var request : Job = launch{
//            repeat(3){ i ->
//                launch {
//                    delay((i + 1) * 200L)
//                    println("coroutines i = $i  is done")
//                }
//
//            }
//            println("request: I'm done and I don't explicitly join my children that are still active")
//        }
//
//        request.join() // 等待请求的完成，包括其所有子协程
//        println("Now processing of the request is complete")
//
//    }
//
//}
//
//
//
//fun nameCoroutineAsync(){
//    runBlocking {
//        logs("Started main coroutine")
//        // 运行两个后台值计算
//        val v1 = async(CoroutineName("v1coroutine")) {
//            delay(500)
//            logs("Computing v1")
//            252
//        }
//        val v2 = async(CoroutineName("v2coroutine")) {
//            delay(1000)
//            logs("Computing v2")
//            6
//        }
//        logs("The answer for v1 / v2 = ${v1.await() / v2.await()}")
//    }
//
//
//}
//
////[main] Started main coroutine
////[main] Computing v1
////[main] Computing v2
////[main] The answer for v1 / v2 = 42
//
//
//fun subLaunchJob(){
//    runBlocking {
//        // 启动一个协程来处理某种传入请求（request）
//        val request = launch {
//            // 生成了两个子作业
//            launch(Job()) {
//                log("job1: I run in my own Job and execute independently!")
//                delay(1000)
//                log("job1: I am not affected by cancellation of the request")
//            }
//
//            // 另一个则承袭了父协程的上下文
//            launch {
//                delay(100)
//                log("job2: I am a child of the request coroutine")
//                delay(1000)
//                log("job2: I will not execute this line if my parent request is cancelled")
//            }
//        }
//
//        delay(500)
//        request.cancel() // 取消请求（request）的执行
//        log("main: Who has survived request cancellation?")
//        delay(1000) // 主线程延迟一秒钟来看看发生了什么
//    }
//
//}
//
//
//
//fun threadJob(){
//
//    runBlocking {
//        // My job is BlockingCoroutine{Active}@4bf558aa
//        println("My job is ${coroutineContext[Job]}")
//    }
//}
//
//
//
//
//fun switchThreadContext(){
//
//    newSingleThreadContext("Ctx1").use { ctx1 ->
//
//        newSingleThreadContext("Ctx2").use { ctx2 ->
//
//            runBlocking(ctx1) {
//                log("Started in ctx1")
//
//                withContext(ctx2) {
//                    log("Working in ctx2")
//                }
//
//                log("Back to ctx1")
//            }
//        }
//    }
//
//    /*
//            [Ctx1] Started in ctx1
//            [Ctx2] Working in ctx2
//            [Ctx1] Back to ctx1
//     */
//
//
//}
//
//
//
//
//
//
//
//fun multiAsync(){
//    runBlocking {
//        val a = async {
//            logs("I'm computing a piece of the answer")
//            6
//        }
//        val b = async {
//            logs("I'm computing another piece of the answer")
//            7
//        }
//        logs("The answer is ${a.await() * b.await()}")
//
//    }
//}
//
//
//
//fun logs(msg: String) = println("[${Thread.currentThread().name}] $msg")
//
//
//
//
//fun launchDelayDemo(){
//    runBlocking {
//        launch {
//            println(" default 前 launch;I'm working in thread name = ${Thread.currentThread().name}")
//            delay(1000)
//            println(" default 后 launch;I'm working in thread name = ${Thread.currentThread().name}")
//        }
//        // 注意这里delay前后的threadName
//        launch(Dispatchers.Unconfined) {
//            println(" Unconfined 前 launch;I'm working in thread name = ${Thread.currentThread().name}")
//            delay(1000)
//            println(" Unconfined 后 launch;I'm working in thread name = ${Thread.currentThread().name}")
//        }
//    }
//}
///*
//            Unconfined 前 launch;I'm working in thread name = main
//            default 前 launch;I'm working in thread name = main
//
//            Unconfined 后 launch;I'm working in thread name = kotlinx.coroutines.DefaultExecutor
//            default 后 launch;I'm working in thread name = main
//
// */
//
//
//
//fun dispatchLaunchSample(){
//    runBlocking {
//        launch {
//            println("main default launch;I'm working in thread name = ${Thread.currentThread().name}")
//        }
//        launch(Dispatchers.Unconfined) {
//            println("Dispatchers.Unconfined default launch;I'm working in thread name = ${Thread.currentThread().name}")
//        }
//        launch(Dispatchers.Default) {
//            println("Dispatchers.Default default launch;I'm working in thread name = ${Thread.currentThread().name}")
//        }
//
//        launch(newSingleThreadContext("myCreateThread")) {
//            println("my create Thread default launch;I'm working in thread name = ${Thread.currentThread().name}")
//        }
//
//
//    }
//
//
//}
//    /*
//            Dispatchers.Unconfined default launch;I'm working in thread name = main
//            Dispatchers.Default default launch;I'm working in thread name = DefaultDispatcher-worker-1
//            my create Thread default launch;I'm working in thread name = myCreateThread
//            main default launch;I'm working in thread name = main
//
//     */
//
//
//
